<template>
  <div class="login-container">
    <!--
      el-form 表单组件
      每个表单项都必须使用 el-form-item 组件包裹
    -->

    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form class="login-form" :model="form" :rules="rules" ref="myForm">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
          <el-checkbox v-model="form.isAgree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary"  :loading="isLoading" @click="hLogin" >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '密码格式不正确', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => {
              console.log(rule, value)
              if (value) {
                // 用户同意
                callback()
              } else {
                callback(new Error('请勾选用户协议!'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      form: {
        mobile: '13911111111', // 手机号
        code: '246810', // 验证码
        isAgree: false // 是否同意协议的选中状态
      },
      isLoading: false
    }
  },
  methods: {
    hLogin () {
      console.log(this.$refs.myForm)
      this.$refs.myForm.validate(result => {
        console.log(result)
        // 验证通过
        if (result) {
          this.login()
        }
      })
    },
    async login () {
      // 3. 向后端服务器发请求，带上参数
      // 把isLoading设成true
      this.isLoading = true
      // axios 就是填空题。答案在接口文档中
      try {
        const result = await this.$http({
          method: 'POST',
          url: '/mp/v1_0/authorizations',
          data: {
            mobile: this.form.mobile,
            code: this.form.code
          }
        })
        // (1) 提示
        this.$message({
          message: '登陆成功',
          type: 'success'
        })
        this.isLoading = false
        //  (2) 保存token
        localStorage.setItem('token', result.data.data.token)
        // （3）成功 -> 跳转
        // 路由转转
        this.$router.push('/')
      } catch (err) {
        //  (2) 失败 -> 提示
        console.log(err)
        // alert('error')
        this.$message({
          message: '登陆失败',
          type: 'error'
        })
        this.isLoading = false
      }
    }
  }
}
</script>
<style lang="less">
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
.login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
